using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Options;
using Publicuse.Appication;
using Publicuse.Autofac;
using Publicuse.Entity;
using Publicuse.Entity.AttributeUtil;
using Publicuse.Export;
using Publicuse.Interface;
using Publicuse.Interface;
using Publicuse.Interface.Extend;
using Publicuse.Util;
using Geekeradmin.Authorize;

namespace Geekeradmin.Controllers
{
  /// <summary>
  /// 黑白名单管理模块
  /// </summary>
  [UserAuthorize("Admin")]
  public class WhitesinglenameController : BaseController
  {
    public WhitesinglenameController(
      IAutofaceInstance autoface,
      IMySqlSugarService mySqlSugar,
      IWebHostEnvironment webHost,
      IOptions<DBConnectionOption> options,
      IBaseServiceExt baseServiceExt
    )
      : base(autoface, mySqlSugar, webHost, options, baseServiceExt) { }

    /// <summary>
    /// 获取黑白名单详细信息
    /// </summary>
    /// <param name="Id"></param>
    /// <returns></returns>
    [HttpGet("GetListb")]
    public async Task<JsonResult> GetDetail(int Id)
    {
      CommonResult result = await WhitesinglenameApplication
        .GetApplication(this.container)
        .GetDetail(Id);
      return new JsonResult(result);
    }

    /// <summary>
    /// 获取黑白名单管理模块信息列表
    /// </summary>
    /// <param name="query"></param>
    /// <returns></returns>
    ///
    [HttpPost("GetList")]
    public async Task<JsonResult> GetList(Whitesinglename query)
    {
      CommonResult result = await WhitesinglenameApplication
        .GetApplication(this.container)
        .GetList(query);
      return new JsonResult(result);
    }

    /// <summary>
    /// 检查黑白名单管理模块IP地址是否存在
    /// </summary>
    /// <param name="IPAddress"></param>
    /// <param name="loginUserCode"></param>
    /// <param name="Id"></param>
    /// <returns></returns>
    [HttpGet("CheckIPAddress"), AllowAnonymous]
    public async Task<JsonResult> CheckIPAddress(
      string IPAddress,
      string loginUserCode,
      string Id = ""
    )
    {
      CommonResult result = await WhitesinglenameApplication
        .GetApplication(this.container)
        .CheckIPAddress(IPAddress, loginUserCode, Id);
      return new JsonResult(result);
    }

    /// <summary>
    /// 检查黑白名单管理模块IP编码是否存在
    /// </summary>
    /// <param name="IPCode"></param>
    /// <param name="loginUserCode"></param>
    /// <param name="Id"></param>
    /// <returns></returns>
    [HttpGet("CheckIPCode"), AllowAnonymous]
    public async Task<JsonResult> CheckIPCode(string IPCode, string loginUserCode, string Id = "")
    {
      CommonResult result = await WhitesinglenameApplication
        .GetApplication(this.container)
        .CheckIPCode(IPCode, loginUserCode, Id);
      return new JsonResult(result);
    }

    /// <summary>
    /// 新增修改黑白名单管理模块信息
    /// </summary>
    /// <param name="info"></param>
    /// <returns></returns>
    [HttpPost("AddOrUpdate")]
    public async Task<JsonResult> AddOrUpdate(Whitesinglename info)
    {
      CommonResult result = await WhitesinglenameApplication
        .GetApplication(this.container)
        .AddOrUpdate(info);
      return new JsonResult(result);
    }

    /// <summary>
    /// 黑白名单管理数据导入
    /// </summary>
    /// <returns></returns>
    [HttpPost("ImportExcel")]
    public async Task<JsonResult> ImportExcel()
    {
      string path = this.webHost.WebRootPath;
      var files = Request.Form.Files;
      string loginUserCode = Request.Form["loginUserCode"];
      CommonResult result = await WhitesinglenameApplication
        .GetApplication(this.container)
        .ImportExcel(files, path, loginUserCode);
      return new JsonResult(result);
    }

    /// <summary>
    /// 批量审核数据
    /// </summary>
    /// <param name="data"></param>
    /// <param name="loginUserCode"></param>
    /// <returns></returns>
    [HttpGet("ConfirmAll"), AllowAnonymous]
    public async Task<JsonResult> ConfirmAll(string data, string loginUserCode)
    {
      CommonResult result = await WhitesinglenameApplication
        .GetApplication(this.container)
        .ConfirmAll(data, loginUserCode);
      return new JsonResult(result);
    }

    /// <summary>
    /// 审核数据
    /// </summary>
    /// <param name="id"></param>
    /// <param name="status"></param>
    /// <param name="loginUserCode"></param>
    /// <returns></returns>
    [HttpGet("Confirm"), AllowAnonymous]
    public async Task<JsonResult> Confirm(string id, int status, string loginUserCode)
    {
      CommonResult result = await WhitesinglenameApplication
        .GetApplication(this.container)
        .Confirm(id, status, loginUserCode);
      return new JsonResult(result);
    }

    /// <summary>
    /// 删除黑白名单管理模块信息
    /// </summary>
    /// <param name="Id"></param>
    /// <param name="loginUserCode"></param>
    /// <param name="type"></param>
    /// <returns></returns>
    [HttpGet("Delete")]
    public async Task<JsonResult> Delete(string Id, string loginUserCode, string type = "-9")
    {
      CommonResult result = await WhitesinglenameApplication
        .GetApplication(this.container)
        .Delete(Id, loginUserCode, type);
      return new JsonResult(result);
    }

    /// <summary>
    /// 还原黑白名单管理模块删除信息
    /// </summary>
    /// <param name="Id"></param>
    /// <param name="loginUserCode"></param>
    /// <returns></returns>
    [HttpGet("Reduction"), AllowAnonymous]
    public async Task<JsonResult> Reduction(string Id, string loginUserCode)
    {
      CommonResult result = await WhitesinglenameApplication
        .GetApplication(this.container)
        .Reduction(Id, loginUserCode);
      return new JsonResult(result);
    }

    /// <summary>
    /// 导出数据信息
    /// </summary>
    /// <param name="query"></param>
    /// <returns></returns>
    [HttpGet("ExportData")]
    public async Task<IActionResult> ExportData([FromQuery] Whitesinglename query)
    {
      return await base.InitExportData(
        this.webHost,
        this.mySqlSugar,
        query,
        "\\template\\report\\whitesinglename.xls",
        "黑白名单信息息统计",
        (a, b, c) =>
        {
          var ms = WhitesinglenameExport.ExportData(a, b, c, out var msgb);
          return ms;
        }
      );
    }
  }
}
